/*
 * @Author: wangze@linkstec.com
 * @Date: 2022-02-28 17:50:05
 * @Function: Please Input Function
 */
// import { Loading, Message } from 'element-ui';
import numeral from 'numeral';
import defaultSettings from '@/settings';

const Loading = ELEMENT.Loading;
const Message = ELEMENT.Message;

const title = defaultSettings.title;

var loadingInstance = null;
export const loading = (flag) => {
    if (flag) {
        loadingInstance = Loading.service({
            lock: false,
            background: 'rgba(0, 0, 0, 0.1)'
        });
    } else {
        if (loadingInstance != null) {
            loadingInstance.close();
        }
    }
};

export const notify = (txt, type) => {
    Message({
        message: txt,
        type: type || 'error'
    });
};

export function getPageTitle (pageTitle) {
    if (pageTitle) {
        return `${pageTitle} - ${title}`;
    }
    return `${title}`;
}

export function getRoutesRoles (routes = []) {
  function flatChildren (routes, result = []) {
    if (routes.length === 0) {
      return result;
    } else {
      const children = routes.map(item => {
        return item.children || [];
      }).flat();
      result.push(...routes.map(item => {
        const { name, meta = {} } = item;
        return {
          [name]: meta.roles ? [...meta.roles] : []
        };
      }));
      return flatChildren(children, result);
    }
  }
  return flatChildren(routes).reduce((result, next) => {
    return {
      ...result,
      ...next
    };
  }, {});
}
